package com.longge;

/**
 * @创建人 xinglongge
 * @创建时间 2021/11/18
 * @描述
 */
public class FindTilt {

    int count = 0;

    //二叉树的坡度
    public int findTilt(TreeNode root) {

        dfs(root);
        return count;
    }

    private int dfs(TreeNode root) {
        if (root == null) return 0;
        int leftVal = dfs(root.left);
        int rightVal = dfs(root.right);
        count += Math.abs(leftVal - rightVal);
        return leftVal + rightVal + root.val;
    }

}
